<template>
  <pro-dialog class="setdialog" ref="probjectModal" width="628" :nopadding="true" :title="addAndExit ? '新增' : '编辑'">
    <div style="padding: 32px 50px 32px 40px">
      <YsForm ref="probjectFrom" :model="projectInfo" :rules="app_rule" label-position="top">
        <YsRow :gutter="16">
          <YsCol span="12">
            <YsFormItem label="项目名称" prop="pd_name">
              <YsInput size="large" :maxlength="20" v-model="projectInfo.pd_name"></YsInput>
            </YsFormItem>
          </YsCol>
          <YsCol span="12">
            <YsFormItem label="项目编号" prop="pd_code">
              <YsInput size="large" :maxlength="20" v-model="projectInfo.pd_code"></YsInput>
            </YsFormItem>
          </YsCol>
        </YsRow>
        <YsRow :gutter="16">
          <YsCol span="12">
            <YsFormItem label="委托(制造单位)" prop="pd_customer">
              <YsInput size="large" :maxlength="20" v-model="projectInfo.pd_customer"></YsInput>
            </YsFormItem>
          </YsCol>
          <YsCol span="12">
            <YsFormItem label="检测单位" prop="pd_customer">
              <YsInput size="large" :maxlength="20" v-model="projectInfo.pd_customer"></YsInput>
            </YsFormItem>
          </YsCol>
        </YsRow>
        <YsRow :gutter="16">
          <YsCol span="12">
            <YsFormItem label="执行标准" prop="pd_customer">
              <YsInput size="large" :maxlength="20" v-model="projectInfo.pd_customer"></YsInput>
            </YsFormItem>
          </YsCol>
          <YsCol span="12">
            <YsFormItem label="完成日期" prop="pd_customer">
              <YsDatePicker type="date" size="large" @on-change="projectInfo.production_date = $event" :transfer="true"
                format="yyyy-MM-dd" v-model="projectInfo.production_date" style="width: 100%"></YsDatePicker>
            </YsFormItem>
          </YsCol>
        </YsRow>
        <YsRow :gutter="16">
          <YsCol span="24">
            <YsFormItem label="备注" prop="remark">
              <YsInput v-model="projectInfo.remark" :class="curTheme" class="input-border" show-word-limit
                type="textarea" maxlength="30" placeholder="请输入" :autosize="{ minRows: 4, maxRows: 5 }"></YsInput>
            </YsFormItem>
          </YsCol>
        </YsRow>
      </YsForm>
      <YsRow class="foot-btn" style="margin-top: 10px">
        <YsButton @click="handleCancel()" size="large">关闭</YsButton>
        <YsButton @click="handleOk('probjectFrom')" :loading="btnDisabled" size="large" type="primary">确定
        </YsButton>
      </YsRow>
    </div>
  </pro-dialog>
</template>

<script>
import { mapState, mapActions } from "vuex"
import setTime from '@/utils/setTime'
export default {
  name: "projectModal",
  computed: {
    ...mapState(['curTheme']),
  },
  props: {
    data: Object,
    addAndExit: Boolean
  },
  data() {
    return {
      btnDisabled: false,
      projectInfo: {
        pd_code: "",
        pd_name: "",
        pd_customer: "",
        remark: "",
      },
      app_rule: {
        pd_code: [
          { required: true, message: "项目编号不能为空", trigger: "blur" },
        ],
        pd_name: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
      },
    }
  },
  methods: {
    ...mapActions(['addSpringProjectAPI', 'editSpringProjectAPI']),
    //确认
    handleOk(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (this.addAndExit) {
            this.addProbjectItem()
          } else {
            this.exitProbjectItem()
          }
        }
      })
    },
    //取消
    handleCancel() {
      this.$refs.probjectModal.close()
    },
    //新增
    addProbjectItem() {
      let _this = this
      this.btnDisabled = true
      if (typeof _this.projectInfo.production_date === "object") {
        _this.projectInfo.production_date = setTime.getNowFormatDate(_this.projectInfo.production_date)
      }
      _this.addSpringProjectAPI({ projectInfo: _this.projectInfo }).then((res) => {
        let { msg, status } = res
        this.btnDisabled = false
        if (status === "ok") {
          _this.$YsMessage.success("新增成功")
          _this.$emit("handleRefresh")
          _this.$refs.probjectModal.close()
        } else {
          _this.$YsMessage.error(msg)
        }
      }).catch((err) => {
        _this.$YsMessage.error(err)
      })
    },
    //编辑
    exitProbjectItem() {
      let _this = this
      this.btnDisabled = true
      if (typeof _this.projectInfo.production_date === "object") {
        _this.projectInfo.production_date = setTime.getNowFormatDate(_this.projectInfo.production_date)
      }
      _this.editSpringProjectAPI({ projectInfo: _this.projectInfo }).then((res) => {
        let { data, msg, scode, status } = res
        this.btnDisabled = false
        if (status === "ok") {
          _this.$YsMessage.success("编辑成功")
          _this.$emit("handleRefresh")
          _this.$refs.probjectModal.close()
        } else {
          _this.$YsMessage.error(msg)
        }
      }).catch((err) => {
        _this.$YsMessage.error(err)
      })
    },
    // 打开窗口
    open() {
      this.$refs.probjectFrom.resetFields()
      this.$refs.probjectModal.show()
    }
  },
  watch: {
    data(nv) {
      this.projectInfo = Object.assign(this.projectInfo, nv)
    }
  }
}
</script>

<style lang="less" scoped>

</style>